//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using DotNetNuke.Modules.NavigationProvider;
namespace DotNetNuke.UI.WebControls
{
	public class SolPartActions : UI.Containers.ActionBase
	{
		private NavigationProvider m_objControl;
		private string m_strProviderName = "SolpartMenuNavigationProvider";
		private string m_strPathSystemScript;
		private bool m_blnPopulateNodesFromClient = false;
		private int m_intExpandDepth = -1;
		public string ProviderName
		{
			get { return m_strProviderName; }
			set { m_strProviderName = value; }
		}
		public bool PopulateNodesFromClient
		{
			get { return m_blnPopulateNodesFromClient; }
			set { m_blnPopulateNodesFromClient = value; }
		}
		public int ExpandDepth
		{
			get
			{
				if (PopulateNodesFromClient == false || Control.SupportsPopulateOnDemand == false)
				{
					return -1;
				}
				return m_intExpandDepth;
			}
			set { m_intExpandDepth = value; }
		}
		public NavigationProvider Control
		{
			get { return m_objControl; }
		}
		public string PathSystemScript
		{
			get { return m_strPathSystemScript; }
			set { m_strPathSystemScript = value; }
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			SetMenuDefaults();
		}
		private void SetMenuDefaults()
		{
			try
			{
				Control.StyleIconWidth = 15;
				Control.MouseOutHideDelay = 500;
				Control.MouseOverAction = NavigationProvider.HoverAction.Expand;
				Control.MouseOverDisplay = NavigationProvider.HoverDisplay.None;
				Control.CSSControl = "ModuleTitle_MenuBar";
				Control.CSSContainerRoot = "ModuleTitle_MenuContainer";
				Control.CSSNode = "ModuleTitle_MenuItem";
				Control.CSSIcon = "ModuleTitle_MenuIcon";
				Control.CSSContainerSub = "ModuleTitle_SubMenu";
				Control.CSSBreak = "ModuleTitle_MenuBreak";
				Control.CSSNodeHover = "ModuleTitle_MenuItemSel";
				Control.CSSIndicateChildSub = "ModuleTitle_MenuArrow";
				Control.CSSIndicateChildRoot = "ModuleTitle_RootMenuArrow";
				if (String.IsNullOrEmpty(Control.PathSystemScript))
					Control.PathSystemScript = Common.Globals.ApplicationPath + "/Controls/SolpartMenu/";
				Control.PathImage = Common.Globals.ApplicationPath + "/Images/";
				Control.PathSystemImage = Common.Globals.ApplicationPath + "/Images/";
				Control.IndicateChildImageSub = "action_right.gif";
				Control.IndicateChildren = true;
				Control.StyleRoot = "background-color: Transparent; font-size: 1pt;";
				Control.NodeClick += ctlActions_MenuClick;
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void ctlActions_MenuClick(NavigationEventArgs args)
		{
			try
			{
				ProcessAction(args.ID);
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		public void BindMenu()
		{
			BindMenu(Navigation.GetActionNodes(this.ActionRoot, this, this.ExpandDepth));
		}
		private void BindMenu(DNNNodeCollection objNodes)
		{
			this.Visible = DisplayControl(objNodes);
			if (this.Visible)
			{
				this.Control.ClearNodes();
				foreach (DNNNode objNode in objNodes)
				{
					ProcessNodes(objNode);
				}
				Control.Bind(objNodes);
			}
		}
		private void ProcessNodes(DNNNode objParent)
		{
			if (!String.IsNullOrEmpty(objParent.JSFunction))
			{
				objParent.JSFunction = string.Format("if({0}){{{1}}};", objParent.JSFunction, Page.ClientScript.GetPostBackEventReference(Control.NavigationControl, objParent.ID));
			}
			foreach (DNNNode objNode in objParent.DNNNodes)
			{
				ProcessNodes(objNode);
			}
		}
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			try
			{
				BindMenu();
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		protected override void OnInit(System.EventArgs e)
		{
			m_objControl = NavigationProvider.Instance(this.ProviderName);
			Control.PopulateOnDemand += new NavigationProvider.PopulateOnDemandEventHandler(Control_PopulateOnDemand);
			base.OnInit(e);
			Control.ControlID = "ctl" + this.ID;
			Control.Initialize();
			this.Controls.Add(Control.NavigationControl);
		}
		private void Control_PopulateOnDemand(NavigationEventArgs args)
		{
			SetMenuDefaults();
			ActionRoot.Actions.AddRange(Actions);
			Entities.Modules.Actions.ModuleAction objAction = ActionRoot;
			if (ActionRoot.ID != Convert.ToInt32(args.ID))
			{
				objAction = this.ActionManager.GetAction(Convert.ToInt32(args.ID));
			}
			if (args.Node == null)
			{
				args.Node = Navigation.GetActionNode(args.ID, Control.ID, objAction, this);
			}
			this.Control.ClearNodes();
			this.BindMenu(Navigation.GetActionNodes(objAction, args.Node, this, this.ExpandDepth));
		}
	}
}
